<script setup lang="ts">
import {message, Modal} from "ant-design-vue";
import {WxtBizCode, WxtUtils} from "~/contentScripts/views/wxt/lib/wxtUtils";

const title = '批量删除人群';
const props = defineProps({
  keywords: {
    type: Array,
    default: () => [],
  },
  bizCode: {
    type: String as PropType<WxtBizCode>,
    default: WxtBizCode.关键字推广,
  },
});
const emit = defineEmits(['refresh']);
const handle = () => {
  if (props.keywords.length === 0) {
    message.error('请勾选需要删除的关键字!');
    return;
  }
  Modal.confirm({
    title: '删除关键字',
    content: '确定删除选中数据吗?',
    onOk: () => {
      const bidwordIdList:any[] = [];
      const wordPackageList:any[] = [];
      props.keywords.forEach((c: any) => {
        if (c.type=='kw'){
          bidwordIdList.push(c.bidwordId);
        }else {
          wordPackageList.push({
            adgroupId: c.adgroupId,
            campaignId: c.campaignId,
            wordPackageId: c.wordPackageId,
            wordPackageName: c.wordPackageName,
            wordPackageType: c.wordPackageType,
          });
        }
        // if (c.type=='zb'){
        //   wordPackageList.push({
        //     adgroupId: c.adgroupId,
        //     campaignId: c.campaignId,
        //     wordPackageId: c.wordPackageId,
        //     wordPackageName: c.wordPackageName,
        //     wordPackageType: c.wordPackageType,
        //   });
        // }
      });
      if (bidwordIdList.length === 0 && wordPackageList.length === 0) {
        message.error('没有需要删除的关键词!');
        return;
      }
      if (bidwordIdList.length > 0){
        WxtUtils.customRequest('https://one.alimama.com/bidword/delete.json', props.bizCode,{
          bidwordIdList: bidwordIdList,
        }).then(() => {
          message.success('删除成功!');
          emit('refresh');
        });
      }
      if (wordPackageList.length > 0){
        WxtUtils.customRequest('https://one.alimama.com/wordPackage/delete.json', props.bizCode,{
          wordPackageList: wordPackageList,
        }).then(() => {
          message.success('删除成功!');
          if (bidwordIdList.length === 0)
            emit('refresh');
        });
      }
    },
  });
}
</script>

<template>
  <div @click="handle">
    <slot>
      <a-button type="primary" ghost danger size="small">批量删除</a-button>
    </slot>
  </div>
</template>

<style scoped>

</style>
